package net.dgg.rz.api.production.controller;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import net.dgg.framework.tac.utils.bean.DggRestResponse;
import net.dgg.framework.tac.utils.web.commmon.DggBaseController;
import net.dgg.rz.api.production.service.ApiProductionService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>@Title 生产部分对外接口controller</p>
 * <p>@Version 1.0.0 版本号</p>
 * <p>@author zyou</p>
 * <p>Copyright © dgg group.All Rights Reserved. 版权信息</p>
 */
@Controller
@RequestMapping("/api/rzsc/production")
public class ApiProductionController extends DggBaseController {


    @Resource
    private ApiProductionService apiProductionService;


    //---------------  其他接口 -----------------------

    /**
     * <p>@Description 接口专用: 根据销售订单ID查询所有生产订单信息</p>
     * <p>@author zyou</p>
     * <p>@Time 2019/2/26 12:00 创建时间</p>
     * <p>@return</p>
     */
    @RequestMapping(value = "/query_product_order_by_order_id", method = RequestMethod.POST )
    @ResponseBody
    public DggRestResponse queryProductOrderByOrderId(@RequestParam Long orderId) {
        try {
            String data = apiProductionService.queryProductOrderByOrderId(orderId);
            return this.getSuccessResponse(data);
        }  catch (Exception e) {
            e.printStackTrace();
            return this.getFailResponse("0");
        }
    }

    /**
     * <p>@Description  根据销售订单ID查询所有完结生产订单放款信息</p>
     * <p>@author zyou</p>
     * <p>@Time 2019/3/12 创建时间</p>
     * <p>@return</p>
     */
    @RequestMapping(value = "/query_loan_money_by_order_id", method = RequestMethod.POST )
    @ResponseBody
    public DggRestResponse queryLoanMoneyByOrderId(@RequestParam Long orderId) {
        try {
            String data = apiProductionService.queryLoanMoneyByOrderId(orderId);
            return this.getSuccessResponse(data);
        }  catch (Exception e) {
            e.printStackTrace();
            return this.getFailResponse("0");
        }
    }

    /**
     * @Title:
     * @Description: 会员中心通过销售订单ID获取生产订单最新节点信息
     * @param:
     * @return:
     * @throw:
     * @author: zyou
     * @creat_date: 2019/2/27
     **/
    @ResponseBody
    @RequestMapping(value = "v1/get_scorder_latest_node_byorderid.do", method = RequestMethod.POST)
    @ApiOperation(value = "会员中心获取生产订单最新节点信息", notes = "会员中心获取生产订单最新节点信息接口", httpMethod = "POST")
    public DggRestResponse getScOrderLatestNodeByOrderId(@ApiParam(name = "orderId", value = "销售订单ID", required = true) @RequestParam Long orderId) {
        try {
            if (null != orderId) {
                Map params = new HashMap<>();
                params.put("orderId", orderId);
                Map scOrderMap = apiProductionService.getScOrderLatestNodeByOrderId(params);
                return this.getSuccessResponse(scOrderMap);
            } else {
                return this.getFailResponse("销售订单ID不能为空");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return this.getFailResponse("操作失败：" + e.getMessage());
        }
    }


    /**
     * @Title:
     * @Description:  会员中心通过销售订单ID获取生产订单业务办理进度
     * @param:, method = RequestMethod.POST
     * @return:
     * @throw:
     * @author: zyou
     * @creat_date: 2019/2/27
     **/
    @ResponseBody
    @RequestMapping(value = "v1/get_scorder_bus_progress_byorderid.do", method = RequestMethod.POST)
    @ApiOperation(value = "会员中心获取生产订单业务办理进度", notes = "会员中心获取生产订单业务办理进度接口", httpMethod = "POST")
    public DggRestResponse getScOrderBusProgressByOrderId(@ApiParam(name = "orderId", value = "销售订单ID", required = true) @RequestParam Long orderId) {
        try {
            if (null != orderId) {
                Map params = new HashMap<>();
                params.put("orderId", orderId);
                List<List<Map<String, Object>>> scOrderListMap = apiProductionService.queryBusProgress(orderId);
                return this.getSuccessResponse(scOrderListMap);
            } else {
                return this.getFailResponse("销售订单ID不能为空");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return this.getFailResponse("操作失败：" + e.getMessage());
        }
    }


    /**
     * 刷新号码加密问题
     */
    @ResponseBody
    @RequestMapping(value = "v1/refresh_no.do", method = RequestMethod.GET)
    @ApiOperation(value = "刷新", notes = "刷新", httpMethod = "GET")
    public DggRestResponse refreshNo(@ApiParam(name = "key", value = "key", required = true) @RequestParam String key) {
        try {
            if(!"updateRefreshNo".equals(key)){
                return this.getFailResponse("操作失败");
            }
            //查询总共有多少条需要刷新的数据，分为5000条一次来刷新
            long startTime=System.currentTimeMillis();   //获取开始时间
            int count = apiProductionService.queryJsonCount();
            int num = count/5000+1;
            for (int i=0;i<num;i++){
                apiProductionService.updateJson();
            }

            long endTime=System.currentTimeMillis(); //获取结束时间
            long time = endTime-startTime;
            System.out.println("耗时： "+time+"ms");
            return this.getSuccessResponse("耗时：" +time+"ms");
        } catch (Exception e) {
            e.printStackTrace();
            return this.getFailResponse("操作失败：" + e.getMessage());
        }
    }


}
